﻿// -----------------------------------------------------------------------
// <copyright file="Format.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AwesomeUtils
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Data;
    using System.Text.RegularExpressions;
    using System.Globalization;
    using System.Web;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Format
    {

        public static string FormatUrl(string Url)
        {
            return FormatUrl(Url, Url);
        }
        public static string FormatUrl(string Url, string LinkText)
        {
            return string.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                !Url.ToLower().StartsWith("http://") ? string.Format("http://{0}", Url) : Url, LinkText);
        }


        public static string FormatEmail(string Email)
        {
            return FormatEmail(Email, Email, string.Empty);
        }
        public static string FormatEmail(string Email, string LinkText)
        {
            return FormatEmail(Email, LinkText, string.Empty);
        }
        public static string FormatEmail(string Email, string LinkText, string Subject)
        {
            return string.Format(@"<a href=""mailto:{0}"">{1}</a>",
                Email + (Subject != string.Empty ? "?subject=" + Subject : string.Empty),
                LinkText);
        }


        public static string FormatHTMLAddress(string Address1, string Address2, string City, string State, string Zip, bool isOneLine)
        {
            string result = string.Empty;

            if (isOneLine)
            {
                result = string.Format("{0} {1} {2}{3} {4}",
                    Address1, Address2, City,
                    State != string.Empty ? (Zip != string.Empty ? ", " + State : string.Empty) : string.Empty, //Accounts for if there is no Zip 
                    Zip);
            }
            else
            {
                result = string.Format(@"
                            <div>{0}</div>
                            <div>{1}</div>
                            <div>{2}{3} {4}</div>",
                Address1,
                Address2,
                City,
                State != string.Empty ? (Zip != string.Empty ? ", " + State : string.Empty) : string.Empty, //Accounts for if there is no Zip
                Zip);
            }
            return result;

        }


        public static string FormatPhoneSkype(string PhoneNumber)
        {
            return FormatPhoneSkype(PhoneNumber, PhoneNumber);
        }
        public static string FormatPhoneSkype(string PhoneNumber, string LinkText)
        {
            return string.Format(@"{1} <a href=""skype:+1{0}?call"" onclick=""return skypeCheck();"" title=""SkypeOut™"" type=""application/x-skype""><img src=""http://download.skype.com/share/skypebuttons/buttons/call_green_transparent_70x23.png"" alt=""Skype Me™!"" /></a>",
                PhoneNumber.Trim().Replace(".", "").Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""),
                LinkText);
        }


        public static string FormatGoogleMapHTML(string Address, string City, string State, string Zip, string LinkText)
        {
            return FormatGoogleMapHTML(Address, City, State, Zip, LinkText, string.Empty);
        }
        public static string FormatGoogleMapHTML(string Address, string City, string State, string Zip, string LinkText, string OrginZip)
        {
            string fulladdress = HttpContext.Current.Server.HtmlEncode(FormatHTMLAddress(Address, string.Empty, City, State, Zip, true));
            return string.Format(@"<a href=""{0}"" title=""Google Maps™"" target=""_blank"" >{1} </a>",
                OrginZip != string.Empty ?
                    string.Format(@"http://maps.google.com/maps?daddr={0}&saddr={1}", fulladdress, OrginZip) :
                    string.Format(@"http://maps.google.com/maps?f=q&hl=en&q={0}", fulladdress),
                LinkText);
        }


        public static string StripHtml(string html)
        {
            return StripHtml(html, false);
        }
        public static string StripHtml(string html, bool allowHarmlessTags)
        {
            if (string.IsNullOrEmpty(html))
                return string.Empty;

            if (allowHarmlessTags) //i believe these need to be defined manually
                return Regex.Replace(html, "", string.Empty);

            return System.Web.HttpUtility.HtmlDecode(Regex.Replace(html, "<[^>]*>", string.Empty.Replace("&nbsp;", string.Empty).Replace("<br>", "\r"))).Trim();
        }


        public static string MakeFirstLetterCaps(string text)
        {
            return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text == null ? "" : text);
        }


        public static string CommaTicEncapsulate(string CSVText)
        {
            string result = string.Empty;
            string[] arr = CSVText.Split(',');
            for (int i = 0; i < arr.Length; i++)
            {
                if (result == string.Empty) result = arr[i];
                else result += string.Format(",'{0}'", arr[i]);
            }
            return result;
        }
        public static DataTable ParseCSV2DT(string inputString)
        {

            DataTable dt = new DataTable();

            // declare the Regular Expression that will match versus the input string
            Regex re = new Regex("((?<field>[^\",\\r\\n]+)|\"(?<field>([^\"]|\"\")+)\")(,|(?<rowbreak>\\r\\n|\\n|$))");

            ArrayList colArray = new ArrayList();
            ArrayList rowArray = new ArrayList();

            int colCount = 0;
            int maxColCount = 0;
            string rowbreak = "";
            string field = "";

            MatchCollection mc = re.Matches(inputString);

            foreach (Match m in mc)
            {

                // retrieve the field and replace two double-quotes with a single double-quote
                field = m.Result("${field}").Replace("\"\"", "\"");

                rowbreak = m.Result("${rowbreak}");

                if (field.Length > 0)
                {
                    colArray.Add(field);
                    colCount++;
                }

                if (rowbreak.Length > 0)
                {

                    // add the column array to the row Array List
                    rowArray.Add(colArray.ToArray());

                    // create a new Array List to hold the field values
                    colArray = new ArrayList();

                    if (colCount > maxColCount)
                        maxColCount = colCount;

                    colCount = 0;
                }
            }

            if (rowbreak.Length == 0)
            {
                // this is executed when the last line doesn't
                // end with a line break
                rowArray.Add(colArray.ToArray());
                if (colCount > maxColCount)
                    maxColCount = colCount;
            }

            // create the columns for the table
            for (int i = 0; i < maxColCount; i++)
                dt.Columns.Add(String.Format("col{0:000}", i));

            // convert the row Array List into an Array object for easier access
            Array ra = rowArray.ToArray();
            for (int i = 0; i < ra.Length; i++)
            {

                // create a new DataRow
                DataRow dr = dt.NewRow();

                // convert the column Array List into an Array object for easier access
                Array ca = (Array)(ra.GetValue(i));

                // add each field into the new DataRow
                for (int j = 0; j < ca.Length; j++)
                    dr[j] = ca.GetValue(j);

                // add the new DataRow to the DataTable
                dt.Rows.Add(dr);
            }

            // in case no data was parsed, create a single column
            if (dt.Columns.Count == 0)
                dt.Columns.Add("NoData");

            return dt;
        }


        public static string ArrayList2CSV(ArrayList al)
        {
            string result = string.Empty;
            foreach (string s in al)
            {
                if (result == string.Empty) result = s;
                else result += string.Format(",{0}", s);
            }
            return result;
        }


        public static string MaskCCNumbers(string data)
        {
            string result = string.Empty;
            char[] charArr = data.ToCharArray();

            if (data.Length > 5)
            {
                result += charArr[0];
                for (int i = 1; i < charArr.Length - 4; i++)
                {
                    result += "*";
                }
                result += data.Substring(data.Length - 4);
            }
            else
            {
                foreach (char ch in charArr)
                {
                    result += "*";
                }
            }

            return result;
        }





    }
}
